﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS.Resources;
using PMS.Win;
using PMS.Utils;
using PMS.ValidationRules;

namespace PMS
{
    public partial class ChangePassword : FormEntryBase
    {
        #region Constants
        private const string BINDING_PROPERTY = "EditValue";
        #endregion

        #region Private Members
        protected AccountBO m_Service;
        #endregion

        #region Properties
        #endregion

        #region Services
        public AccountBO Service
        {
            get
            {
                if (m_Service == null)
                {
                    m_Service = new AccountBO();
                }
                return m_Service;
            }
        }
        #endregion

        #region Constructors
        public ChangePassword(Account user)
        {
            InitializeComponent();
            Init();
            CurrentUser = user;
        }
        #endregion

        #region Init Data and Style
        private void Init()
        {
            InitData();
            InitStyle();
        }

        private void InitData()
        {
        }

        private void InitStyle()
        {
            txtOldPass.Tag = "Ignore";
            txtNewPass.Tag = "Ignore";
            txtConfirm.Tag = "Ignore";

            txtOldPass.BackColor = REQUIRED_FIELD_COLOR;
            txtNewPass.BackColor = REQUIRED_FIELD_COLOR;
            txtConfirm.BackColor = REQUIRED_FIELD_COLOR;
        }
        #endregion

        #region override methods

        protected override void BindingToControls(IBaseEntity entity)
        {
            base.BindingToControls(entity);

        }

        protected override void FocusFirtsControl()
        {
            base.FocusFirtsControl();
            this.txtOldPass.Focus();
        }

        protected override void InitValidationRules()
        {
            base.InitValidationRules();
            Validator validate = new Validator(ValidationProvider);
            validate.AddValidationRule(base.CreateNotEmptyValidationRule());
            SetValidator(validate, txtOldPass);
            SetValidator(validate, txtNewPass);
            
        }

        public override void New()
        {
            
        }

        protected override bool Save()
        {
            try
            {
                if (Validate())
                {
                    string newpass = DataConvertUltils.ToString(txtNewPass.EditValue);
                    CurrentUser.Matkhau = newpass; 
                    Service.ChangePassword(CurrentUser.ID,newpass);
                }
                return true;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
                return false;
            }
        }

        protected override bool Delete()
        {
            //your code here
            return true;
        }

        public override void Cancel()
        {
            //your code here
            //base.Cancel();
        }

        protected override IBaseEntity GetEntity()
        {
            //need to get the latest record of the current Entity's ID or code then return it
            return base.GetEntity();
        }
        #endregion


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (DoSave())
            {
                ShowSuccessMessage("ChangePasswordSuccess");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private bool Validate()
        {
            string oldpass = DataConvertUltils.ToString(txtOldPass.EditValue);
            string newpass = DataConvertUltils.ToString(txtNewPass.EditValue);
            string confirm = DataConvertUltils.ToString(txtConfirm.EditValue);

            if (string.IsNullOrEmpty(oldpass))
            {
                ShowErrorMessage(Messages.GetString("OldPasswordIsEmpty"));
                return false;
            }
            if (string.IsNullOrEmpty(newpass))
            {
                ShowErrorMessage(Messages.GetString("NewPasswordIsEmpty"));
                return false;
            }

            if (oldpass.CompareTo(CurrentUser.Matkhau) != 0)
            {
                ShowErrorMessage(Messages.GetString("PasswordIncorrect"));
                return false;
            }
            else
            {
                if (newpass.CompareTo(confirm) != 0)
                {
                    ShowErrorMessage(Messages.GetString("ConfirmPasswordIsNotMatch"));
                    return false;
                }
            }

            return true;
        }


        #region Lookups
        #endregion

        #region Form's function
        #endregion
    }
}
